﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.UIElements;
using Image = UnityEngine.UI.Image;

public class Root : MonoBehaviour
{
    // Start is called before the first frame update
    public Transform mMapParent;
    public GameObject mPrefabPlot;
    private GameObject mRole;

    private GameObject[,] mPlots;
    void Start()
    {
        int[,] array =
        {
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
            {1, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1},
            {1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1},
            {1, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 1},
            {1, 1, 1, 0, 0, 0, 1, 0, 1, 1, 0, 1},
            {1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1},
            {1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1},
            {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
        };
        
        mPlots = new GameObject[8,12];
        this.RefMap(array);
        
        Maze maze = new Maze(array);
        Point start = new Point(1,1);
        Point end = new Point(2, 6);
        var parent = maze.FindPath(start, end, false);
        Debug.Log("Print path:");
        while (parent != null)
        {
            Debug.Log(parent.X + ", " + parent.Y);
            Debug.Log("value:"+array[parent.X,parent.Y]);
            this.mPlots[parent.X,parent.Y].transform.Find("Go").gameObject.SetActive(true);
            parent = parent.ParentPoint;
           
        }
       
    }

    void RefMap(int[,]array)
    {
        int len = array.Length;
        Debug.Log("len:"+len);
        for (int x = 0; x < 8; x++)
        {
            for (int y = 0; y < 12; y++)
            {
                GameObject go =GameObject.Instantiate(this.mPrefabPlot);
                this.RefPlot(go,array[x,y],x,y);
                this.mPlots[x, y] = go;
                go.name = x + "," + y;
            }
        }
    }

    void RefPlot(GameObject go,int state,int x ,int y)
    {
        Image image = go.transform.Find("Image").GetComponent<Image>();
        Color color =Color.white;
        if(state == 1)
            color = Color.blue;
        image.color = color;
        
        Text text = go.transform.Find("Text").GetComponent<Text>();
        text.text = x + "," + y;
        
        go.transform.SetParent(this.mMapParent);
        
        Vector3 pos = this.GetPos(x, y);
        go.transform.position = pos;
        go.transform.localScale = Vector3.one;
        go.SetActive(true);
      
    }

    Vector3 GetPos(int x, int y)
    {
        float posX = 50 + x * 100;
        float posY = 50 + y * 100;
        return new Vector3(posX,posY);
    }
}
